package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.uuid.IdUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.system.enums.SystemMessageEnum;
import com.qys.livesMall.system.model.dto.sys.SysLanguageTextDTO;
import com.qys.livesMall.system.model.entity.SysLanguageText;
import com.qys.livesMall.system.mapper.SysLanguageTextMapper;
import com.qys.livesMall.system.model.vo.sys.SysLanguageTextVO;
import com.qys.livesMall.system.service.SysLanguageTextService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 多语言文本 服务实现类
 * </p>
 *
 * @author
 * @since 2023-09-25
 */
@Service
public class SysLanguageTextServiceImpl extends ServiceImpl<SysLanguageTextMapper, SysLanguageText> implements SysLanguageTextService {

    @Override
    public boolean add(SysLanguageTextDTO sysLanguageTextDTO) {
        if (sysLanguageTextDTO.getTypeId()==null){
            sysLanguageTextDTO.setTypeId(IdUtils.fastSimpleUUID());
        }
        sysLanguageTextDTO.setId(null);
        SysLanguageText sysLanguageText=new SysLanguageText();
        BeanUtil.copyProperties(sysLanguageTextDTO,sysLanguageText);
        return save(sysLanguageText);
    }

    @Override
    public boolean updateInfo(SysLanguageTextDTO sysLanguageTextDTO) {
        SysLanguageText sysLanguageText=getById(sysLanguageTextDTO.getId());
        if (null == sysLanguageText){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        if (sysLanguageTextDTO.getTypeId()==null){
            sysLanguageTextDTO.setTypeId(IdUtils.fastSimpleUUID());
        }
        BeanUtil.copyProperties(sysLanguageTextDTO,sysLanguageText);
        return updateById(sysLanguageText);
    }

    @Override
    public IPage<SysLanguageTextVO> queryPage(BaseQuery query) {
        LambdaQueryWrapper<SysLanguageText> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getName()),SysLanguageText::getTypeName,query.getName());
        queryWrapper.orderByDesc(SysLanguageText::getId);
        IPage<SysLanguageTextVO> page=page(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper).convert(e -> {
            SysLanguageTextVO sysLanguageTextVO=new SysLanguageTextVO();
            BeanUtil.copyProperties(e,sysLanguageTextVO);
            return sysLanguageTextVO;
        });
        return page;
    }

    @Override
    public SysLanguageTextVO getInfo(Long id) {
        SysLanguageText sysLanguageText=getById(id);
        if (null == sysLanguageText){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        SysLanguageTextVO sysLanguageTextVO=new SysLanguageTextVO();
        BeanUtil.copyProperties(sysLanguageText,sysLanguageTextVO);
        return sysLanguageTextVO;
    }

    @Override
    public List<SysLanguageTextVO> getTypeAll(String name) {
        LambdaQueryWrapper<SysLanguageText> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),SysLanguageText::getTypeName,name);
        queryWrapper.groupBy(SysLanguageText::getTypeId);
        List<SysLanguageText> list=list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)){
            List<SysLanguageTextVO> textVOS=new ArrayList<>();
            for (SysLanguageText text:list){
                SysLanguageTextVO sysLanguageTextVO=new SysLanguageTextVO();
                BeanUtil.copyProperties(text,sysLanguageTextVO);
                textVOS.add(sysLanguageTextVO);
            }
            return textVOS;
        }
        return null;
    }

    @Override
    public String getInfoBytypeId(String typeId) {
        LambdaQueryWrapper<SysLanguageText> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysLanguageText::getTypeId,typeId);
        queryWrapper.eq(SysLanguageText::getLanguage, SecurityUtils.getLanguage());
        List<SysLanguageText> list=list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)){
            return list.get(0).getContent();
        }
        return null;
    }

    @Override
    public String getByLangAndTypeId(String typeId, String lang) {
       String msg= getInfoBytypeId(typeId);
       if (msg==null){
           LambdaQueryWrapper<SysLanguageText> queryWrapper=new LambdaQueryWrapper<>();
           queryWrapper.eq(SysLanguageText::getTypeId,typeId);
           queryWrapper.eq(SysLanguageText::getLanguage, lang);
           List<SysLanguageText> list=list(queryWrapper);
           if (CollectionUtils.isNotEmpty(list)){
               return list.get(0).getContent();
           }
       }
        return null;
    }
}
